/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rentalmobil.entity;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import rentalmobil.koneksidatabase.koneksiDataBase;

/**
 *
 * @author Henfryandie
 */
public class dataMobil {

    int no_mobil;
    String no_plat_mobil;
    String kondisi_Mobil;
    String status_mobil;
    int tarif_sewa;

    public dataMobil() {
    }

    public dataMobil(int no_mobil, String no_plat_mobil, String kondisi_Mobil, String status_mobil, int tarif_sewa) {
        this.no_mobil = no_mobil;
        this.no_plat_mobil = no_plat_mobil;
        this.kondisi_Mobil = kondisi_Mobil;
        this.status_mobil = status_mobil;
        this.tarif_sewa = tarif_sewa;
    }

    public int getNo_mobil() {
        return no_mobil;
    }

    public void setNo_mobil(int no_mobil) {
        this.no_mobil = no_mobil;
    }

    public String getKondisi_Mobil() {
        return kondisi_Mobil;
    }

    public void setKondisi_Mobil(String kondisi_Mobil) {
        this.kondisi_Mobil = kondisi_Mobil;
    }

    public String getNo_plat_mobil() {
        return no_plat_mobil;
    }

    public void setNo_plat_mobil(String no_plat_mobil) {
        this.no_plat_mobil = no_plat_mobil;
    }

    public String getStatus_mobil() {
        return status_mobil;
    }

    public void setStatus_mobil(String status_mobil) {
        this.status_mobil = status_mobil;
    }

    public int getTarif_sewa() {
        return tarif_sewa;
    }

    public void setTarif_sewa(int tarif_sewa) {
        this.tarif_sewa = tarif_sewa;
    }

    public boolean dataMobilView() throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM datamobil;");
        while (rs.next()) {
            setNo_mobil(rs.getInt("idMobil"));
            setNo_plat_mobil(rs.getString("no_plat_mobil"));
            setKondisi_Mobil(rs.getString("kondisi_mobil"));
            setStatus_mobil(rs.getString("status_mobil"));
            setTarif_sewa(rs.getInt("tarif_sewa_mobil"));
        }
        st.close();
        return true;
    }

    public void updateStatusMobilSewa(String NoPlat) throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        int rs = st.executeUpdate("UPDATE `rentalmobil2011`.`datamobil` SET `status_mobil`='Disewa' WHERE `no_plat_mobil`='" + NoPlat + "';");
        st.close();
    }

    public void updateStatusMobilKembali(String NoPlat) throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        int rs = st.executeUpdate("UPDATE `rentalmobil2011`.`datamobil` SET `status_mobil`='Ada' WHERE `no_plat_mobil`='" + NoPlat + "';");
        st.close();
    }

    public void updateKondisiMobil(String NoPlat) throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        int rs = st.executeUpdate("UPDATE `rentalmobil2011`.`datamobil` SET `Kondisi_mobil`='Bagus' WHERE `no_plat_mobil`='" + NoPlat + "';");
        st.close();
    }

    public List<dataMobil> viewDataMobil() throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT no_plat_mobil,Kondisi_mobil,status_mobil,tarif_sewa_mobil FROM datamobil;");
        List<dataMobil> list = new ArrayList<dataMobil>();
        while (rs.next()) {
            dataMobil dm = new dataMobil();
            dm.setNo_plat_mobil(rs.getString("no_plat_mobil"));
            dm.setKondisi_Mobil(rs.getString("Kondisi_mobil"));
            dm.setStatus_mobil(rs.getString("status_mobil"));
            dm.setTarif_sewa(rs.getInt("tarif_sewa_mobil"));
            list.add(dm);
        }
        return list;
    }

    public boolean LaporanMobilView(String noPlatmobil) throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT Kondisi_mobil FROM datamobil WHERE no_plat_mobil='" + noPlatmobil + "';");
        while (rs.next()) {
            setKondisi_Mobil(rs.getString("Kondisi_mobil"));
        }
        return true;
    }

    public boolean insertDataMobil() throws SQLException {
        PreparedStatement ins = koneksiDataBase.getConnection().prepareStatement("INSERT INTO datamobil(no_plat_mobil,Kondisi_mobil,status_mobil,tarif_sewa_mobil) VALUES ( ?,?,?,?);");
        ins.setString(1, getNo_plat_mobil());
        ins.setString(2, getKondisi_Mobil());
        ins.setString(3, getStatus_mobil());
        ins.setInt(4, getTarif_sewa());
        ins.executeUpdate();
        return true;

    }

    public boolean updateDataMobil() throws SQLException {
        PreparedStatement update = koneksiDataBase.getConnection().prepareStatement("UPDATE datamobil SET Kondisi_mobil=?,status_mobil=?, tarif_sewa_mobil=? WHERE no_plat_mobil=?;");
        update.setString(1, getKondisi_Mobil());
        update.setString(2, getStatus_mobil());
        update.setInt(3, getTarif_sewa());
        update.setString(4, getNo_plat_mobil());
        update.executeUpdate();
        return true;
    }

    public List<dataMobil> viewDataNoMobil() throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT no_plat_mobil FROM datamobil WHERE Kondisi_mobil = 'Rusak' ");
        List<dataMobil> list = new ArrayList<dataMobil>();
        while (rs.next()) {
            dataMobil dm = new dataMobil();
            dm.setNo_plat_mobil(rs.getString("no_plat_mobil"));
            list.add(dm);
        }
        return list;
    }

    public List<dataMobil> viewDataNoMobilForTransaksiSewa() throws SQLException {
        Statement st = koneksiDataBase.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT no_plat_mobil FROM datamobil WHERE status_mobil NOT LIKE 'Disewa' AND Kondisi_mobil NOT LIKE 'Rusak' AND status_mobil NOT LIKE 'Di Bengkel';");
        List<dataMobil> list = new ArrayList<dataMobil>();
        while (rs.next()) {
            dataMobil dm = new dataMobil();
            dm.setNo_plat_mobil(rs.getString("no_plat_mobil"));
            list.add(dm);
        }
        return list;
    }
}
